import { ADD_POST, GET_POSTS, GET_POST, DELETE_POST, POST_LOADDING } from '../actions/types'
const initialState = {
  posts: [],//多个评论
  post: {},//单个评论或留言
  loading: false
};
export default function (state = initialState, action) {
  switch (action.type) {
    case POST_LOADDING:
      return {
        ...state,
        loading: true
      }
    case GET_POSTS://请求评论内容
      return {
        ...state,
        posts: action.payload,
        loading: false
      }
    case GET_POST://获取鼓励留言信息
      return {
        ...state,
        post: action.payload,
        loading: false
      }
    case ADD_POST:
      return {
        ...state,
        posts: [action.payload, ...state.posts]
      }

    case DELETE_POST://删除单个评论
      return {
        ...state,
        posts: state.posts.filter(post => post._id !== action.payload)//过滤掉不相等的id,也就是删除掉已点击的删除的id
      }
    default:
      return state;
  }
}